package com.link.base.base.fund.service;

import com.link.base.base.fund.dao.mybatis.mapper.DealerTransactionDetailMapper;
import com.link.base.base.fund.model.BillDetailModel;
import com.link.base.base.fund.model.DealerAccount;
import com.link.base.base.fund.model.DealerTransactionDetail;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.ServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 经销商资金交易明细,增加注释
 *
 * @author 黄龙建
 * @date 2020/1/8 5:46 下午
 */
@Service
public class DealerTransactionDetailServiceImpl extends BasicServiceImpl<DealerTransactionDetail> implements DealerTransactionDetailService {

    @Resource
    private DealerTransactionDetailMapper dealerTransactionDetailMapper;

    @Resource
    private DealerAccountService dealerAccountService;
    private Long deleteHeadId;

    @Override
    public BasicMapper<DealerTransactionDetail> getBasicMapper() {
        return dealerTransactionDetailMapper;
    }

    @Override
    public List<DealerTransactionDetail> queryByPeriod(DealerTransactionDetail detail) {
        return dealerTransactionDetailMapper.queryByPeriod(detail);
        //return null;
    }

    /**
     * HuangLJ 重写父的afterUpdate 方法，确保每一次更新行的时候。头都重新汇总一次
     *
     * @param dealerTransactionDetail 资金账户行记录
     * @throws Exception 常规报错
     */
    @Override
    public void afterUpdate(DealerTransactionDetail dealerTransactionDetail) throws Exception {
        //头Id
        Long id = dealerTransactionDetail.getId();
        if (null == id) {
            throw new ServiceException("PUBLIC-007");
        }
        DealerTransactionDetail dealerTransactionDetailQuery = new DealerTransactionDetail();
        dealerTransactionDetailQuery.setId(id);
        DealerTransactionDetail dealerTransactionDetailNew = dealerTransactionDetailMapper.queryById(dealerTransactionDetailQuery);
        if (dealerTransactionDetailNew == null) {
            throw new ServiceException("FUND-018");
        }

        DealerAccount dealerAccount = dealerAccountService.queryById(dealerTransactionDetailNew.getHeadId());
        String type = dealerAccount.getType();
        Long accntId = dealerAccount.getAccntId();

        DealerAccount dealerAccountQuery = new DealerAccount();
        dealerAccountQuery.setType(type);
        dealerAccountQuery.setAccntId(accntId);
        DealerAccount dealerAccountSum = dealerAccountService.queryItemSumByTypeAndAccntId(dealerAccountQuery);

        dealerAccountService.updateAmount(dealerAccountSum);
    }

    /**
     * HuangLJ 重写父的afterInsert 方法，确保每一次新建行的时候。头都重新汇总一次
     *
     * @param dealerTransactionDetail 资金账户行记录
     * @throws Exception 常规报错
     */
    @Override
    public void afterInsert(DealerTransactionDetail dealerTransactionDetail) throws Exception {
        Long headId = dealerTransactionDetail.getHeadId();
        if (null == headId) {
            throw new ServiceException("FUND-019");
        }
        DealerAccount dealerAccount = dealerAccountService.queryById(headId);
        String type = dealerAccount.getType();
        Long accntId = dealerAccount.getAccntId();

        DealerAccount dealerAccountQuery = new DealerAccount();
        dealerAccountQuery.setType(type);
        dealerAccountQuery.setAccntId(accntId);
        dealerAccountQuery.setId(headId);
        DealerAccount dealerAccountSum = dealerAccountService.queryItemSumByTypeAndAccntId(dealerAccountQuery);
        dealerAccountService.updateAmount(dealerAccountSum);
    }

    /**
     * HuangLJ 重写父的afterDelete 方法，确保每一次删除行的时候。头都重新汇总一次
     *
     * @param dealerTransactionDetail 资金账户行记录
     * @throws Exception 常规报错
     */
    @Override
    public void afterDelete(DealerTransactionDetail dealerTransactionDetail) throws Exception {
        DealerAccount dealerAccount = dealerAccountService.queryById(this.deleteHeadId);
        String type = dealerAccount.getType();
        Long accntId = dealerAccount.getAccntId();
        Long id = dealerAccount.getId();

        DealerAccount dealerAccountQuery = new DealerAccount();
        dealerAccountQuery.setType(type);
        dealerAccountQuery.setAccntId(accntId);
        dealerAccountQuery.setId(id);
        DealerAccount dealerAccountSum = dealerAccountService.queryItemSumByTypeAndAccntId(dealerAccountQuery);
        dealerAccountService.updateAmount(dealerAccountSum);
    }

    /**
     * HuangLJ 重写父的afterDelete 方法，确保每一次删除行的时候。头都重新汇总一次
     *
     * @param dealerTransactionDetail 资金账户行记录
     * @throws Exception 常规报错
     */
    @Override
    public void beforDelete(DealerTransactionDetail dealerTransactionDetail) throws Exception {
        DealerTransactionDetail dealerTransactionDetailOld = dealerTransactionDetailMapper.queryById(dealerTransactionDetail);
        this.deleteHeadId = dealerTransactionDetailOld.getHeadId();
    }

    /***
     * 查询关联企业下指定条件的交易明细
     * @param billDetailModel
     * @return list 列表
     * @throws Exception
     */
    @Override
    public List<BillDetailModel> queryAllByMultiCond(BillDetailModel billDetailModel) throws Exception {
        return dealerTransactionDetailMapper.queryAllByMultiCond(billDetailModel);
    }

    /**
     * 通过客户和期间值查询交易明细
     *
     * @param billDetailModel
     * @return list 列表
     * @throws Exception
     */
    @Override
    public List<BillDetailModel> queryAllByPerAndAccnt(BillDetailModel billDetailModel) throws Exception {
        return dealerTransactionDetailMapper.queryAllByPerAndAccnt(billDetailModel);
    }


    /**
     * 查询交易余额
     *
     * @param actionType      操作类型
     * @param accntId         操作客户
     * @param dealerAccntType 资金账户类型
     * @param periodStartDate 开始期间
     * @param periodEndDate   结束期间
     * @param sourceTypes     排除的交易类型集合
     * @return billDetailModel 实例
     * @throws Exception
     */
    @Override
    public BillDetailModel queryBalanceByPerAndAccnt(String actionType, Long accntId,
                                                     String dealerAccntType, String periodStartDate,
                                                     String periodEndDate, List<String> sourceTypes) throws Exception {
        BillDetailModel bdm = new BillDetailModel();
        bdm.setList(sourceTypes);
        bdm.setAccntId(accntId);
        bdm.setPeriodStartDate(periodStartDate);
        bdm.setPeriodEndDate(periodEndDate);
        bdm.setActionType(actionType);
        bdm.setDealerAccntType(dealerAccntType);
        return dealerTransactionDetailMapper.queryBalanceByPerAndAccnt(bdm);
    }
}
